ggplot etc - Yt Part 1 and Part 2 - P2 is more interesting line #197.
INFO526 Term Project
Author
Affiliation
Derek Rice
School of Information, University of Arizona
# cite: https://www.youtube.com/watch?v=h29g21z0a68&t=290s # just a review, should probably be more focused on maps and interactive maps, a quick review data("faithful")ggplot(data = faithful,mapping =aes(x = eruptions, y = waiting, colour = eruptions)) +geom_point()
# colour dist. in two difference colours ggplot(faithful) +geom_histogram(aes(x = eruptions, fill = eruptions <3.1))
# colour based on weight > 60# view(faithful) # two para. eruptions and waiting ggplot(faithful) +geom_histogram(aes(x = eruptions, fill = waiting <60, position ="identity", alpha =0.5))
# add a line that separates the two point dist.# how to draw a straight lines from a slope and intercept ggplot(faithful) +geom_point(aes(x = eruptions, y = waiting)) +geom_abline(slope =15, intercept =25, colour ="red", linewidth =3)
view(mpg)
ggplot(mpg) +geom_bar(aes(x = class, fill =as.factor(cyl)), colour ="black")
mpg_counted <- mpg |>count(class, name ="count")view(mpg_counted)ggplot(mpg_counted) +geom_bar(aes(x = class, y = count),stat ='identity') #identity is just passing the data along
ggplot(mpg) +geom_density(aes(x = hwy, y =after_stat(scaled)))
ggplot(mpg) +geom_jitter(aes(x = class, y = hwy), width =0.2)
ggplot(mpg) +geom_point(aes(x = displ, y = hwy, colour = class) ) +scale_colour_brewer(name ='CLASS', type ='qual', palette ='Set1')
ggplot(mpg) +geom_bar(aes(x = class)) +coord_polar(theta ='y') +expand_limits (y =70) # set max on raidial dial
# zoom in on data - set limits on scale ggplot(mpg)+geom_bar(aes(x = class)) +scale_y_continuous(limits =c(0,40)) #removes data
ggplot(mpg)+geom_bar(aes(x = class)) +coord_cartesian(ylim =c(0,40)) #coord_cartesian does NOT remove data
# then work on theme but nothing new
# cite: https://www.youtube.com/watch?v=0m4yywqNPVY&t=296s# Part 2# ggplot## 47 geoms## 25 stats## 62 scales p1 <-ggplot(msleep) +geom_boxplot(aes(x = sleep_total, y = vore, fill = vore))p2 <-ggplot(msleep) +geom_bar(aes(y = vore, fill = vore))p3 <-ggplot(msleep) +geom_point(aes(x = bodywt,y = sleep_total,colour = vore)) +scale_x_log10()p3
combined_plot <- p1 + p2 + p3print(combined_plot)
plot_all <- (p1 | p2) / p3print(plot_all)
new_plot <- plot_allnew_plot &theme(legend.position ="none"# removes legend from all plots )
new_plot +plot_annotation(title ="Mammalian Sleep Patterns",tag_levels ="A"# tags plots - can use roman I ) &# must use & and not +theme(legend.position ="none"# removes legend from all plots )
new_plot +plot_annotation(title ="Mammalian Sleep Patterns",tag_levels ="I"# tags plots - can use roman I ) &# must use & and not +theme(legend.position ="none"# removes legend from all plots )
# Plot Composition# stories are told with multiple plots# plot layout hsould be flexible and well aligned # many approaches## facet hacking## gridExtra::grid.arrange()## ggpubr::ggrange()## cowplot::plot_grid()## patchwork <- preferred?
p =ggplot(mtcars) +geom_point(aes(x = disp, y = mpg))p + p + p +plot_layout(width =c(1,2,1))
p + p + p +plot_layout(width =unit(c(5,1,1),c('cm','null','null'))) # null is fill out remaining area
p1 <-ggplot(mtcars[mtcars$gear ==3,]) +geom_point(aes(x = disp, y = mpg))p2 <-ggplot(mtcars[mtcars$gear ==4,]) +geom_point(aes(x = disp, y = mpg))p1 + p2 # axis are not aligned
# how should the textual rep. be understood? ### layout in Patchwork ###p1 <-ggplot(mtcars) +geom_point(aes(x = disp, y = mpg))p2 <-ggplot(mtcars) +geom_bar(aes(x =factor(gear)))p3 <-ggplot(mtcars) +geom_boxplot(aes(x =factor(gear), y = mpg))p1 + p2 + p3
layout <-'AAA #fraction of screen width used for plot AA# = left 2/3 and #AA = right 2/3 and AAA = full width#BB # can also use numbers 1, 2, 3 ... C##'# p1 + p2 + p3 + plot_layout(design = layout)
### animations # ANIMATION # story telling and attention grabbing# may turn boring people off# our eyes are drawn to movement and our mind thinks in motion # many ways to make animaltions - gganimage may be the only one doing the "grammar" way # ggplot2 wrapper - simply provides new grammar elements and takes over rendering ggplot(economics) +geom_line(aes(x = date, y = unemploy))
ggplot(economics) +geom_line(aes(x = date, y = unemploy)) +transition_reveal(along = date)ggplot(mpg) +geom_bar(aes(x =factor(cyl))) +labs(title ="Number of cars in {closest_state} by number of cylinders") +transition_states(states = year) +enter_grow() +exit_fade() ggplot(mpg) +geom_point(aes(x = displ, y = hwy)) +ggtitle("Cars with {closest_state} cylinders") +transition_states(factor(cyl))ggplot(mpg) +geom_point(aes(x = displ, y = hwy, group =seq_len(nrow(mpg)))) +#define discrete groups with group=seq_len()ggtitle("Cars with {closest_state} cylinders") +transition_states(factor(cyl))
## ANNOTATION # two key packages# ggrepel# ggforce ggplot(mtcars, aes(x = disp, y = mpg)) +geom_point() +geom_text_repel(aes(label =row.names(mtcars)) )
# ggforceggplot(mtcars, aes(x = disp, y = mpg)) +geom_point() +geom_mark_ellipse( # there are other options to ellipse, circle, square, hullaes(filter = gear ==4,label ='4 gear cars',description ="cars with fewer gears .. "))
# reduce number of points with labels# ggrepelmtcars2 <- mtcarsmtcars2$label <-rownames(mtcars2)points_to_label <-sample(nrow(mtcars),10) #first ten labels ggplot(mtcars2, aes(x = disp, y = mpg)) +geom_point() +geom_text_repel(aes(label = label), data = mtcars2[points_to_label,])
ggplot(mtcars, aes( x = disp, y = mpg)) +geom_point() +geom_mark_ellipse(aes(fill =factor(gear), label = gear)) # very interesting
# also a package called gghighlight
### Part 3: Beyond GGPLOT2 ######## https://www.youtube.com/watch?v=0m4yywqNPVY&t=3884s# 1:24:00# NETWORKS# GGally# ggnetwork# geomnet# ggtree# ggdag#install.packages("BiocManager")#BiocManager::install("ggtree")# install.packages("ggtree")library(ggtree)library(ape)# for graphically displaying socal networks, the interelatinship between nodes# can plot a hierarchical clusters iris_clust <-hclust(dist(iris[,1:4]))iris_phylo <-as.phylo(iris_clust)ggtree(iris_phylo) +geom_tiplab() +theme_tree2()
# Part 4 - drawing anything# 1:48:00
Source Code
---title: "more review"subtitle: "ggplot etc - Yt Part 1 and Part 2 - P2 is more interesting line #197. "author: - name: "Derek Rice" affiliations: - name: "School of Information, University of Arizona"description: "INFO526 Term Project"format: html: code-tools: true code-overflow: wrap code-line-numbers: true embed-resources: trueeditor: visualcode-annotations: hoverexecute: warning: false---```{r setup, include = FALSE}#| label: load-pkgs#| message: falseknitr::opts_chunk$set(echo = TRUE)if (!require("pacman")) install.packages("pacman")pacman::p_load(tidyverse, dplyr, janitor, lubridate, ggplot2, readr, readxl, patchwork, gganimate, ggrepel, ggforce, GGally, ggnetwork, geomnet, ggtree, ggdag, ggtree, ape )``````{r}# cite: https://www.youtube.com/watch?v=h29g21z0a68&t=290s # just a review, should probably be more focused on maps and interactive maps, a quick review data("faithful")ggplot(data = faithful,mapping =aes(x = eruptions, y = waiting, colour = eruptions)) +geom_point()ggplot() +geom_point(mapping =aes(x = eruptions, y = waiting, colour = eruptions <3),data = faithful)ggplot(faithful) +geom_histogram(aes(x = eruptions, fill = eruptions <3))ggplot(faithful, aes(x = eruptions, y = waiting)) +geom_density_2d() +geom_point()# make points sqr and slightly transparent ggplot(faithful) +geom_point(aes(x = eruptions, y = waiting), shape ='square')ggplot(faithful) +geom_point(aes(x = eruptions, y = waiting), shape ='square', alpha =0.5)# colour dist. in two difference colours ggplot(faithful) +geom_histogram(aes(x = eruptions, fill = eruptions <3.1))# colour based on weight > 60# view(faithful) # two para. eruptions and waiting ggplot(faithful) +geom_histogram(aes(x = eruptions, fill = waiting <60, position ="identity", alpha =0.5))# add a line that separates the two point dist.# how to draw a straight lines from a slope and intercept ggplot(faithful) +geom_point(aes(x = eruptions, y = waiting)) +geom_abline(slope =15, intercept =25, colour ="red", linewidth =3)``````{r}view(mpg)``````{r}ggplot(mpg) +geom_bar(aes(x = class, fill =as.factor(cyl)), colour ="black")mpg_counted <- mpg |>count(class, name ="count")view(mpg_counted)ggplot(mpg_counted) +geom_bar(aes(x = class, y = count),stat ='identity') #identity is just passing the data along ggplot(mpg) +geom_density(aes(x = hwy, y =after_stat(scaled)))ggplot(mpg) +geom_jitter(aes(x = class, y = hwy), width =0.2)ggplot(mpg) +geom_point(aes(x = displ, y = hwy, colour = class) ) +scale_colour_brewer(name ='CLASS', type ='qual', palette ='Set1')ggplot(mpg) +geom_point(aes(x = displ, y = hwy)) +scale_x_continuous(breaks =c(3,5,6)) +scale_y_continuous(trans ='log10')ggplot(mpg) +geom_point(aes(x = displ, y = hwy, colour = class, size = cyl)) +scale_colour_brewer(type ='qual') +scale_size_area(breaks =c(4,5,6,8))``````{r}ggplot(mpg) +geom_point(aes(x = displ, y = hwy)) +facet_wrap(~ class)ggplot(mpg) +geom_point(aes(x = displ, y = hwy)) +facet_grid(year ~ drv)``````{r}# coordinates ggplot(mpg) +geom_bar(aes(x = class)) +coord_polar()ggplot(mpg) +geom_bar(aes(x = class)) +coord_polar(theta ='y')ggplot(mpg) +geom_bar(aes(x = class)) +coord_polar(theta ='y') +expand_limits (y =70) # set max on raidial dial # zoom in on data - set limits on scale ggplot(mpg)+geom_bar(aes(x = class)) +scale_y_continuous(limits =c(0,40)) #removes data ggplot(mpg)+geom_bar(aes(x = class)) +coord_cartesian(ylim =c(0,40)) #coord_cartesian does NOT remove data # then work on theme but nothing new ``````{r}# cite: https://www.youtube.com/watch?v=0m4yywqNPVY&t=296s# Part 2# ggplot## 47 geoms## 25 stats## 62 scales p1 <-ggplot(msleep) +geom_boxplot(aes(x = sleep_total, y = vore, fill = vore))p2 <-ggplot(msleep) +geom_bar(aes(y = vore, fill = vore))p3 <-ggplot(msleep) +geom_point(aes(x = bodywt,y = sleep_total,colour = vore)) +scale_x_log10()p3combined_plot <- p1 + p2 + p3print(combined_plot)plot_all <- (p1 | p2) / p3print(plot_all)new_plot <- plot_allnew_plot &theme(legend.position ="none"# removes legend from all plots ) new_plot +plot_annotation(title ="Mammalian Sleep Patterns",tag_levels ="A"# tags plots - can use roman I ) &# must use & and not +theme(legend.position ="none"# removes legend from all plots ) new_plot +plot_annotation(title ="Mammalian Sleep Patterns",tag_levels ="I"# tags plots - can use roman I ) &# must use & and not +theme(legend.position ="none"# removes legend from all plots ) # Plot Composition# stories are told with multiple plots# plot layout hsould be flexible and well aligned # many approaches## facet hacking## gridExtra::grid.arrange()## ggpubr::ggrange()## cowplot::plot_grid()## patchwork <- preferred? ``````{r}p =ggplot(mtcars) +geom_point(aes(x = disp, y = mpg))p + p + p +plot_layout(width =c(1,2,1))p + p + p +plot_layout(width =unit(c(5,1,1),c('cm','null','null'))) # null is fill out remaining area``````{r}p1 <-ggplot(mtcars[mtcars$gear ==3,]) +geom_point(aes(x = disp, y = mpg))p2 <-ggplot(mtcars[mtcars$gear ==4,]) +geom_point(aes(x = disp, y = mpg))p1 + p2 # axis are not aligned# view(mtcars)p1 + p2 &scale_y_continuous(limits =c(10,40)) &scale_x_continuous(limits =c(50,500))``````{r}# how should the textual rep. be understood? ### layout in Patchwork ###p1 <-ggplot(mtcars) +geom_point(aes(x = disp, y = mpg))p2 <-ggplot(mtcars) +geom_bar(aes(x =factor(gear)))p3 <-ggplot(mtcars) +geom_boxplot(aes(x =factor(gear), y = mpg))p1 + p2 + p3layout <-'AAA #fraction of screen width used for plot AA# = left 2/3 and #AA = right 2/3 and AAA = full width#BB # can also use numbers 1, 2, 3 ... C##'# p1 + p2 + p3 + plot_layout(design = layout)``````{r}### animations # ANIMATION # story telling and attention grabbing# may turn boring people off# our eyes are drawn to movement and our mind thinks in motion # many ways to make animaltions - gganimage may be the only one doing the "grammar" way # ggplot2 wrapper - simply provides new grammar elements and takes over rendering ggplot(economics) +geom_line(aes(x = date, y = unemploy))ggplot(economics) +geom_line(aes(x = date, y = unemploy)) +transition_reveal(along = date)ggplot(mpg) +geom_bar(aes(x =factor(cyl))) +labs(title ="Number of cars in {closest_state} by number of cylinders") +transition_states(states = year) +enter_grow() +exit_fade() ggplot(mpg) +geom_point(aes(x = displ, y = hwy)) +ggtitle("Cars with {closest_state} cylinders") +transition_states(factor(cyl))ggplot(mpg) +geom_point(aes(x = displ, y = hwy, group =seq_len(nrow(mpg)))) +#define discrete groups with group=seq_len()ggtitle("Cars with {closest_state} cylinders") +transition_states(factor(cyl))``````{r}## ANNOTATION # two key packages# ggrepel# ggforce ggplot(mtcars, aes(x = disp, y = mpg)) +geom_point() +geom_text_repel(aes(label =row.names(mtcars)) )# ggrepelggplot(mtcars, aes(x = disp, y = mpg)) +geom_point() +geom_text_repel(aes(label =row.names(mtcars)) )# ggforceggplot(mtcars, aes(x = disp, y = mpg)) +geom_point() +geom_mark_ellipse( # there are other options to ellipse, circle, square, hullaes(filter = gear ==4,label ='4 gear cars',description ="cars with fewer gears .. "))# reduce number of points with labels# ggrepelmtcars2 <- mtcarsmtcars2$label <-rownames(mtcars2)points_to_label <-sample(nrow(mtcars),10) #first ten labels ggplot(mtcars2, aes(x = disp, y = mpg)) +geom_point() +geom_text_repel(aes(label = label), data = mtcars2[points_to_label,])ggplot(mtcars, aes( x = disp, y = mpg)) +geom_point() +geom_mark_ellipse(aes(fill =factor(gear), label = gear)) # very interesting # also a package called gghighlight ``````{r}### Part 3: Beyond GGPLOT2 ######## https://www.youtube.com/watch?v=0m4yywqNPVY&t=3884s# 1:24:00# NETWORKS# GGally# ggnetwork# geomnet# ggtree# ggdag#install.packages("BiocManager")#BiocManager::install("ggtree")# install.packages("ggtree")library(ggtree)library(ape)# for graphically displaying socal networks, the interelatinship between nodes# can plot a hierarchical clusters iris_clust <-hclust(dist(iris[,1:4]))iris_phylo <-as.phylo(iris_clust)ggtree(iris_phylo) +geom_tiplab() +theme_tree2()``````{r}# Part 4 - drawing anything# 1:48:00```